import React, {Component} from 'react';

class SetState extends Component {
    state = {
        count: 0
    }

    add = () => {
        // let {count} = this.state
        // this.setState({
        //     count: count += 1
        // }, () => {
        //     console.log('更新完了')
        //     console.log(count)
        // })
        // let a = this.state.count
        // console.log('a:',a)
        // 函数式更新，因为react是异步更新，回调里面才是最新的值
        this.setState((state,props) => {
            return {count:state.count+=1}
        })
    }

    render() {
        return (
            <div>
                <h1>当前求和：{this.state.count}</h1>
                <button onClick={this.add}>+1</button>
            </div>
        );
    }
}

export default SetState;